<template>
  <client-only>
    <div class="home page-component">
      <el-carousel indicator-position="outside">
        <el-carousel-item v-for="item in 2" :key="item">
          <img src="~assets/images/er.jpg" alt="" />
        </el-carousel-item>
      </el-carousel>
      <!-- 搜索 -->
      <div class="search-container">
        <div class="search-wrapper">
          <div class="hospital-search">
            <el-autocomplete class="search-input inline-input" prefix-icon="el-icon-search" v-model="hosname"
              :fetch-suggestions="querySearchAsync" placeholder="点击输入医院名称" @select="handleSelect">
              <span slot="suffix" class="search-btn v-link highlight clickable selected">搜索
              </span>
            </el-autocomplete>
          </div>
        </div>
      </div>
      <!-- bottom -->
      <div class="bottom">
        <div class="left">
          <div class="home-filter-wrapper">
            <div class="title">医院</div>
            <div>
              <div class="filter-wrapper">
                <span class="label">等级：</span>
                <div class="condition-wrapper">
                  <span class="item v-link clickable" :class="hostypeActiveIndex == -1 ? 'selected' : ''"
                    @click="hostypeSelect(-1, -1)">全部</span>
                  <span class="item v-link clickable" v-for="(item, index) in hostypeList" :key="index"
                    :class="hostypeActiveIndex == index ? 'selected' : ''" @click="hostypeSelect(item.value, index)">
                    {{ item.name }}
                  </span>
                </div>
              </div>

              <div class="filter-wrapper">
                <span class="label">地区：</span>
                <div class="condition-wrapper">
                  <span class="item v-link clickable" :class="provinceActiveIndex == -1 ? 'selected' : ''"
                    @click="districtSelect(-1, -1)">全部</span>
                  <span class="item v-link clickable" v-for="(item, index) in districtList" :key="index"
                    :class="provinceActiveIndex == index ? 'selected' : ''" @click="districtSelect(item.value, index)">
                    {{ item.label }}
                  </span>
                </div>
              </div>
            </div>
          </div>
          <div class="v-scroll-list hospital-list">
            <div v-for="(item, index) in list" :key="index" class="v-card clickable list-item space">
              <div class="">
                <div class="hospital-list-item hos-item" @click="show(item.hoscode)" index="0">
                  <div class="wrapper">
                    <div class="hospital-title">{{ item.hosname }}</div>
                    <div class="bottom-container">
                      <div class="icon-wrapper">
                        <span class="iconfont"></span>
                        {{ item.param.dictName }}
                      </div>
                      <div class="icon-wrapper">
                        <span class="iconfont"></span>
                        每天{{ item.bookingRule.releaseTime }}放号
                      </div>
                    </div>
                  </div>
                  <img :src="'data:image/jpeg;base64,' + item.logoData" :alt="item.hosname" class="hospital-img" />
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="right">
          <div class="common-dept">
            <div class="header-wrapper">
              <div class="title">常见科室</div>
              <div class="all-wrapper">
                <span>全部</span>
                <span class="iconfont icon"></span>
              </div>
            </div>
            <div class="content-wrapper">
              <span class="item v-link clickable dark">神经内科 </span>
              <span class="item v-link clickable dark">消化内科 </span>
              <span class="item v-link clickable dark">呼吸内科 </span>
              <span class="item v-link clickable dark">内科 </span>
              <span class="item v-link clickable dark">神经外科 </span>
              <span class="item v-link clickable dark">妇科 </span>
              <span class="item v-link clickable dark"> 产科 </span>
              <span class="item v-link clickable dark">儿科 </span>
            </div>
          </div>
          <div class="space">
            <div class="header-wrapper">
              <div class="title-wrapper">
                <div class="icon-wrapper">
                  <span class="iconfont title-icon"></span>
                </div>
                <span class="title">平台公告</span>
              </div>
              <div class="all-wrapper">
                <span>全部</span>
                <span class="iconfont icon"></span>
              </div>
            </div>
            <div class="content-wrapper">
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">关于什么放假通知
                </span>
              </div>
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">中医药大学
                </span>
              </div>
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">
                  湘雅医院最新消息
                </span>
              </div>
            </div>
          </div>
          <div class="suspend-notice-list space">
            <div class="header-wrapper">
              <div class="title-wrapper">
                <div class="icon-wrapper">
                  <span class="iconfont title-icon"></span>
                </div>
                <span class="title">停诊公告</span>
              </div>
              <div class="all-wrapper">
                <span>全部</span>
                <span class="iconfont icon"></span>
              </div>
            </div>
            <div class="content-wrapper">
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">
                  协和医院呼吸内科门诊停诊公告
                </span>
              </div>
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">
                  中南医院关于特殊门诊停诊通知
                </span>
              </div>
              <div class="notice-wrapper">
                <div class="point"></div>
                <span class="notice v-link clickable dark">长号医院中西医结合心内科门诊停诊公告
                </span>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </client-only>
</template>

<script>

export default {
  // 服务端渲染异步，显示医院列表
  asyncData ({ $axios, params, error }) {
    // 调用
    let obj = {
      hostype: -1
    }
    return $axios.post(`/admin/hosp/nuxtHospital/list/1/7`, obj)
      .then(({ data }) => {
        return {
          list: data.data.content,
          pages: data.data.totalPages,
        }
      })
  },
  data () {
    return {
      // 搜索框值
      hosname: '',
      // 医院等级数组
      hostypeList: [],
      // 地区数组
      districtList: [],
      //当前选中医院等级索引和value
      hostypeActiveIndex: -1,
      hostype: -1,
      // 当前选中地区索引和省市区
      provinceActiveIndex: -1,
      provinceCode: -1,
      // 医院信息
      list: [],
      pages: 0,
      // 分页需要
      currentPage: 1,
      limit: 7,
    }
  },
  methods: {
    // 搜索框建议方法
    querySearchAsync (queryString, cb) {
      let arr = new Array()
      if (this.hosname != '') {
        this.$axios.get(`/admin/hosp/nuxtHospital/findHospitalByHosname/${this.hosname}`).then(({ data }) => {
          data.data.forEach(e => {
            let allInfos = {
              hoscode: e.hoscode,
              value: e.hosname
            }
            arr.push(allInfos)
          })
        })
      }
      cb(arr)
    },
    // 选择搜索框建议的方法
    handleSelect (item) {
      // 点击进入详情页面
      // 取到hoscode进行跳转查询
      window.location.href = '/hosp/' + item.hoscode
    },
    // 点击选择医院等级
    hostypeSelect (value, index) {
      this.hostypeActiveIndex = index
      this.hostype = value
      this.showHospitalList()
    },
    // 选择地区
    districtSelect (value, index) {
      this.provinceActiveIndex = index
      this.provinceCode = value
      this.showHospitalList()
    },
    //显示详情的方法
    show (hoscode) {
      // 跳转到详情页面
      window.location.href = '/hosp/' + hoscode
    },
    // 展示医院的方法
    showHospitalList () {
      let obj = {
        provinceCode: this.provinceCode,
        cityCode: this.cityCode,
        districtCode: this.districtCode,
        hostype: this.hostype
      }
      // 调用方法查询
      this.$axios.post(`/admin/hosp/nuxtHospital/list/${this.currentPage}/${this.limit}`, obj).then(({ data }) => {
        this.list = data.data.content
        this.pages = data.data.totalPages
      }).catch(e => {
        this.$message.error("医院列表出现错误")
      })
    },
    // 查询医院等级的方法
    showHostype () {
      this.$axios.get(`/admin/cmn/dict/findDictByDictCode/Hostype`).then(({ data }) => {
        this.hostypeList = data.data
      }).catch(e => {
        this.$message.error('医院等级列表出现错误')
      })
    },
    // 查询省市区的方法
    findAllPCA () {
      this.$axios.get(`http://localhost:3000/pca-code.json`).then(({ data }) => {
        this.districtList = data
      }).catch(e => {
        this.$message.error('地区列表出现错误')
      })
    },
  },
  created () {
    this.findAllPCA()
    this.showHostype()
  }
}
</script>
<style>
</style>

